package com.tjy.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.tjy.springboot.beanVo.Result;
import com.tjy.springboot.model.Emp;
import com.tjy.springboot.beanVo.EmpVo;
import com.tjy.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    @ResponseBody
    public List<Emp> findAll(){
        return userService.findAll();
    }


    @RequestMapping("/findPage")
    public String findPage(EmpVo empVo,Model model, Integer page , Integer rows){
        if(page == null ){
            page=1;
        }
        if(rows==null){
            rows=10;
        }
        PageInfo<Emp> pageInfo = userService.findPage(empVo ,page, rows);
        model.addAttribute("page",pageInfo.getList());
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("empVo",empVo);
        return "user";
    }
    @RequestMapping("/saveAdd")
    public String saveAdd(Emp emp){
        userService.saveAdd(emp);
        return "redirect:/user/findPage";
    }
    @RequestMapping("/del/{eid}")
    @ResponseBody
    public Result del(@PathVariable("eid") Integer eid){
      return   userService.del(eid);
    }

    @RequestMapping("/update/{eid}")
    @ResponseBody
    public Emp updateSelect(@PathVariable("eid") Integer eid){
        return   userService.updateSelect(eid);
    }
}
